/*
 * SettingsForm.java
 *
 * Copyright (C) 2005-2006 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * Created on January 5th 2005
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package com.substanceofcode.carmanager.view;

import com.substanceofcode.carmanager.controller.CarManagerMIDlet;
import com.substanceofcode.carmanager.model.CarManagerSettings;
import javax.microedition.lcdui.*;

/**
 *
 * @author  Tommi Laukkanen
 */
public class SettingsForm extends Form implements CommandListener {
    private CarManagerMIDlet m_midlet;
    private Command m_okCommand     = new Command("OK",     Command.OK,     1);
    private Command m_cancelCommand = new Command("Cancel", Command.CANCEL, 2);
    
    /** Fields */
    private ChoiceGroup m_entryTypeGroup;
    
    /** Creates a new instance of SettingsForm */
    public SettingsForm(CarManagerMIDlet midlet) 
    {
        super(null);
        m_midlet = midlet;
        
        /** Add entry type choice group to form */
        String[] entryTypeStrings = {"Total fill-up", 
                                   "Price per litre"};
        m_entryTypeGroup = new ChoiceGroup("Entry type:", ChoiceGroup.EXCLUSIVE, 
                entryTypeStrings, null);
        append(m_entryTypeGroup);
        
        /** Add commands and command listener */
        addCommand(m_okCommand);
        addCommand(m_cancelCommand);
        setCommandListener(this);
    }
    
    /** Set currency symbol field's value */
    public void setSettings(int entryType) {
        m_entryTypeGroup.setSelectedIndex(entryType, true);
    }
    
    /** Handles the OK and Back commands */
    public void commandAction(Command c, Displayable d)
    {
        if( c==m_okCommand )
        {
            /** Get selected entry type and save it to settings */
            int entryType;
            entryType = m_entryTypeGroup.getSelectedIndex();
            CarManagerSettings settings = CarManagerSettings.getInstance( m_midlet );
            settings.setEntryType( entryType );
            
            /** Display the main canvas */
            m_midlet.showMainCanvas();
        }
        else if( c==m_cancelCommand )
        {
            /** Display the main canvas */
            m_midlet.showMainCanvas();
        }
    }

    
}
